#ifndef CH_DRAWHELPERS_H
#define CH_DRAWHELPERS_H

#pragma once

//////////////////////////////////////////////////////////////////////
// CHEmptyRect class

class AFX_EXT_CLASS CHEmptyRect : public CRect
{
public:
	CHEmptyRect()
	{
		SetRectEmpty();
	}
};

//////////////////////////////////////////////////////////////////////
// CHWindowRect class

class AFX_EXT_CLASS CHWindowRect : public CRect
{
public:
	CHWindowRect (HWND hWnd)
	{
		::GetWindowRect (hWnd, this);
	};
	CHWindowRect (const CWnd* pWnd)
	{
		::GetWindowRect (pWnd->GetSafeHwnd(), this);
	};
};

//////////////////////////////////////////////////////////////////////
// CHClientRect class

class AFX_EXT_CLASS CHClientRect : public CRect
{
public:
	CHClientRect (const CWnd* pWnd)
	{
		::GetClientRect (pWnd->GetSafeHwnd(), this);
	};
};

//////////////////////////////////////////////////////////////////////
// CHBufferDC class

class AFX_EXT_CLASS CHBufferDC : public CDC
{
	HDC     m_hDestDC;
	CBitmap m_bitmap;     // Bitmap in Memory DC
	CRect   m_rect;
	HGDIOBJ m_hOldBitmap; // Previous Bitmap
	
public:
	CHBufferDC (HDC hDestDC, const CRect rcPaint) : m_hDestDC (hDestDC)
	{
		m_rect = rcPaint;
		VERIFY(Attach (::CreateCompatibleDC (m_hDestDC)));
		m_bitmap.Attach (::CreateCompatibleBitmap (m_hDestDC, m_rect.right, m_rect.bottom));
		m_hOldBitmap = ::SelectObject (m_hDC, m_bitmap);
	}
	~CHBufferDC ()
	{
		VERIFY(::BitBlt (m_hDestDC, m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(), m_hDC, m_rect.left, m_rect.top, SRCCOPY));
	    ::SelectObject (m_hDC, m_hOldBitmap);
	}
};

//////////////////////////////////////////////////////////////////////
// CHFontDC class

class AFX_EXT_CLASS CHFontDC
{
	CDC*     m_pDC;
	CFont* m_pOldFont;
public:
	CHFontDC (CDC* pDC, CFont* pFont) : m_pDC (pDC)
	{
		m_pOldFont = (CFont*) m_pDC->SelectObject (pFont);
	}
	~CHFontDC()
	{
		m_pDC->SelectObject (m_pOldFont);
	}
};

//////////////////////////////////////////////////////////////////////
// CHPenDC class

class AFX_EXT_CLASS CHPenDC
{
protected:
	CPen m_pen;
	HDC m_hDC;
	HPEN m_hOldPen;
	
public:
	CHPenDC (HDC hDC, COLORREF crColor): m_hDC (hDC)
	{
		VERIFY(m_pen.CreatePen (PS_SOLID, 1, crColor));
		m_hOldPen = (HPEN)::SelectObject (m_hDC, m_pen);
	}
	~CHPenDC ()
	{
		::SelectObject (m_hDC, m_hOldPen);
	}
};

//////////////////////////////////////////////////////////////////////
// CHBrushDC class

class AFX_EXT_CLASS CHBrushDC
{
protected:
	CBrush m_brush;
	HDC m_hDC;
	HBRUSH m_hOldBrush;
	
public:
	CHBrushDC (HDC hDC, COLORREF crColor): m_hDC (hDC)
	{
		VERIFY(m_brush.CreateSolidBrush (crColor));
		m_hOldBrush = (HBRUSH)::SelectObject (m_hDC, m_brush);
	}
	~CHBrushDC ()
	{
		::SelectObject (m_hDC, m_hOldBrush);
	}
};

class AFX_EXT_CLASS CHCompatibleDC : public CDC
{
public:
	CHCompatibleDC(CDC* pDC, CBitmap* pBitmap)
	{
		CreateCompatibleDC(pDC);	
		m_pBmp = SelectObject(pBitmap);
	}
	~CHCompatibleDC()
	{
		SelectObject(m_pBmp);
		DeleteDC();
	}
	CBitmap* m_pBmp;
};


#endif //CH_DRAWHELPERS_H
